package com.ctshk.rpc.iotc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.iotc.dto.OperateBankPreferenceListDTO;
import com.ctshk.rpc.iotc.dto.OperateBankPreferenceQueryDTO;
import com.ctshk.rpc.iotc.dto.OperatePromotionQueryDTO;
import com.ctshk.rpc.iotc.entity.OperateBankPreference;
import com.ctshk.rpc.iotc.entity.OperateBankPreferenceProduct;
import com.ctshk.rpc.iotc.mapper.OperateBankPreferenceMapper;
import com.ctshk.rpc.iotc.mapper.OperateBankPreferenceProductMapper;
import com.ctshk.rpc.iotc.req.IdsTwo;
import com.ctshk.rpc.iotc.req.OperateBankConformQueryReq;
import com.ctshk.rpc.iotc.req.OperateBankConformReq;
import com.ctshk.rpc.iotc.req.OperatePromotionConformReq;
import com.ctshk.rpc.iotc.service.IOperateBankPreferenceProductService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 银联优惠适用产品 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-22
 */
@DubboService
public class OperateBankPreferenceProductServiceImpl extends ServiceImpl<OperateBankPreferenceProductMapper, OperateBankPreferenceProduct> implements IOperateBankPreferenceProductService {

    @Autowired
    private OperateBankPreferenceMapper operateBankPreferenceMapper;

    @Autowired
    private OperateBankPreferenceServiceImpl operateBankPreferenceService;

    private OperateBankPreference operateBankPreference;

    @Override
    public List<OperateBankPreferenceListDTO> list(OperateBankConformReq req) {
        QueryWrapper<OperateBankPreferenceProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OperateBankPreferenceProduct::getRelationId, req.getRelationId());
        queryWrapper.lambda().eq(OperateBankPreferenceProduct::getIsDeleted, IsDeletedCode.NO.getCode());
        List<OperateBankPreferenceProduct> productList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(productList)) {
            return null;
        }
        List<OperateBankPreference> operateBankPreferences = new ArrayList<>();
        return null;
    }

    @Override
    public Result<List<OperateBankPreferenceListDTO>> getOperateBankPreferenceProduct(Long productId) {
        QueryWrapper<OperateBankPreferenceProduct> qw = new QueryWrapper<>();
        qw.lambda().select(OperateBankPreferenceProduct::getCodeId)
                .eq(OperateBankPreferenceProduct::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(OperateBankPreferenceProduct::getRelationId, productId);
        List<OperateBankPreferenceProduct> obpp = baseMapper.selectList(qw);
        if (obpp != null && obpp.size() > 0) {
            List<Long> collect = obpp.stream().map(e -> e.getCodeId()).collect(Collectors.toList());
            if (collect != null && collect.size() > 0) {
                List<OperateBankPreference> result = operateBankPreferenceMapper.selectBatchIds(collect);
                return Result.success(EntityUtil.copyList(result, OperateBankPreferenceListDTO.class));
            }
        }
        return Result.failed(SystemError.IOTC_40009);
    }

    @Override
    public PageResponse<OperateBankPreferenceListDTO> queryListAll(IdsTwo req) {
        Page<OperateBankPreferenceProduct> page = new Page(req.getPageNo(), req.getPageSize());
        QueryWrapper<OperateBankPreferenceProduct> qw = new QueryWrapper<>();
        qw.lambda().select(OperateBankPreferenceProduct::getCodeId)
                .eq(OperateBankPreferenceProduct::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(OperateBankPreferenceProduct::getRelationId, req.getRelationId());
        List<OperateBankPreferenceProduct> obpp = baseMapper.selectList(qw);
        if (obpp != null && obpp.size() > 0) {
            List<Long> collect = obpp.stream().map(e -> e.getCodeId()).collect(Collectors.toList());
            if (collect != null && collect.size() > 0) {
                List<OperateBankPreference> result = operateBankPreferenceMapper.selectBatchIds(collect);
                List<OperateBankPreferenceListDTO> resultPage = EntityUtil.copyList(result, OperateBankPreferenceListDTO.class);
                OperateBankConformQueryReq opcr = new OperateBankConformQueryReq();
                opcr.setBusinessType(req.getBusinessType());
                opcr.setCardNumber(req.getCardNumber());
                opcr.setChannelId(req.getChannelId());
                opcr.setDiscountObjectId(req.getDiscountObjectId());
                opcr.setRelationId(req.getRelationId());
                opcr.setTotalPaidIn(req.getTotalPaidIn());
                opcr.setUserId(req.getUserId());
                resultPage.forEach(e -> {
                    opcr.setId(e.getId());
                    Result<OperateBankPreferenceQueryDTO> res = operateBankPreferenceService.queryConform(opcr);
                    if (res.getData() != null) {
                        e.setMoney(res.getData().getDiscount());
                    }
                });
                resultPage = resultPage.stream().filter(e -> e.getMoney() != null).collect(Collectors.toList());
                return new PageResponse<>(resultPage,
                        page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                        page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
            }
        }
        return new PageResponse<>(null,
                page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
    }
}
